/**
 * 
 */
package org.apache.struts2.gtgrid.components;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * 
 * @author Stevie
 * @since 2009-12-1
 * @version 1.0
 * 
 */
@StrutsTag(name = "gt-grid", tldTagClass = "org.apache.struts2.views.jsp.ui.GtGridTag", description = "Renders Gt-Grid")
public class GtGrid extends UIBean {

	final public static String TEMPLATE = "gtgrid";

	// properties
	protected String id; // 列表id
	protected String width; // 列表宽度 支持%
	protected String height; // 列表高度 支持%
	protected String container; // 列表的容器的id.
								// 列表最后是要显示在页面中某个位置.这个容器就是那个"位置",通常是一个div或者时td.
	protected String replaceContainer; // 是否用列表代替容器元素
	protected String customHead; // 自定义表头对应的table的id或对象,或table的HTML代码
	protected String columns; // 列定义
	protected String dataset; // 列表对应的数据集
	protected String loadURL; // 读取数据的url
	protected String saveURL; // 提交数据的url
	protected String language; // 默认语言
	protected String skin; // 默认皮肤
	protected String toolbarPosition; // 工具栏的位置. 可选值:'top'/'bottom'/null,
										// 暂时不支持上下都显示.
	protected String toolbarContent; // 工具栏上要显示的东西. state表示的数据数量信息(分页信息),如共几条
										// 共几页 当前是第几条等等..toolbarContent 默认=
										// "nav | goto | pagesize | reload | add del save | print | filter chart | state"
										// ,
	protected String resizable; // 是否可手动改变整个列表区域的大小( 默认: false)
	protected String minWidth; // 可手动改变大小时,最小宽度
	protected String minHeight; // 可手动改变大小时,最小高度
	protected String showIndexColumn; // 是否显示索引列
	protected String stripeRows; // 是否交替显示行颜色
	protected String lightOverRow; // 是否开启行的鼠标悬停指示
	protected String defaultRecord; // 默认的记录样板,新增记录时,将按此样板新增.
	protected String transparentMask; // 是否显示全透明的遮罩 (默认: false )
	protected String clickStartEdit; // 是否为"点击单元格直接进入编辑状态"
	protected String reloadAfterSave; // 提交数据后是否自动刷新列表
	protected String recountAfterSave; // 提交数据后是否清空总记录数(以便后台重新统计总记录数)
	protected String showGridMenu; // 是否开启主菜单
	protected String allowCustomSkin; // 是否开启主菜单上的皮肤选择选项
	protected String allowFreeze; // 是否开启主菜单上的冻结列选项
	protected String allowHide; // 是否开启主菜单上的隐藏列选项
	protected String allowGroup; // 是否开启主菜单上的列编组选项
	protected String selectRowByCheck; // false
	protected String submitUpdatedFields; // false

	protected String pageSize; // 每页记录数
	protected String pageSizeList; // [ 25, 50, 100, 200 ]
	protected String remotePaging; // true/false 时候使用远程分页. 如果是客户端分页 要设置成 false,
									// 如果是服务的分页 要写成true.

	// events
	protected String onClickCellTopics; // 在选中某单元格时调用,返回false,则中止选中行为
	protected String onDblClickCellTopics; // 双击单元格时调用
	protected String onClickHeadTopics; // 点击列表头
	protected String onCompleteTopics; // 列表生成后调用
	protected String onShowTopics; // 列表由隐藏状态到显示状态时调用(需要开发人员手动调用,且必须调用)
	protected String onResizeTopics; // 列表大小发生变化时调用
	protected String onMouseMoveTopics; // 鼠标在列表数据区移动时调用的函数
	protected String beforeSelectRowTopics; // 在选中某行前调用,返回false,则中止选中行为
	protected String afterSelectRowTopics; // 选中某行之后调用
	protected String beforeEditTopics; // 编辑单元格前调用(虽然无参数,但是可与 activeCell
										// activeRecord交互)
	protected String afterEditTopics; // 编辑单元格前调用(虽然无参数,但是可与 activeCell
										// activeRecord交互)
	protected String beforeSaveTopics; // 保存数据前前调用 返回false,则中止save
										// 参数为提交的所有参数组成的json对象,结构后面说明
	protected String beforeLoadTopics; // 读取数据前调用 返回false,则中止load
										// 参数为提交的所有参数组成的json对象,结构后面说明
	protected String beforeInsertTopics; // 添加数据前调用 返回false,则中止insert 参数为新增的记录
	protected String afterInsertTopics; // 添加数据后调用
	protected String beforeUpdateTopics; // 修改数据前调用 返回false,则中止update
											// 参数为编辑的记录,编辑的字段,新值
	protected String afterUpdateTopics; // 修改数据后调用
	protected String beforeDeleteTopics; // 删除数据前调用 返回false,则中止del 参数为删除的记录
	protected String afterDeleteTopics; // 删除数据后调用
	protected String onUpdateRecordTopics; // 在记录发生改变时调用
	protected String beforeGotoPageTopics; // 翻页

	public GtGrid(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.components.UIBean#getDefaultTemplate()
	 */
	@Override
	protected String getDefaultTemplate() {
		return TEMPLATE;
	}

	@Override
	public String getTheme() {
		return "ajax";
	}

	public void evaluateExtraParams() {
		super.evaluateExtraParams();
		if (id != null)
			addParameter("id", findString(id));
		if (width != null)
			addParameter("width", findString(width));
		if (height != null)
			addParameter("height", findString(height));
		if (container != null)
			addParameter("container", findString(container));
		if (replaceContainer != null)
			addParameter("replaceContainer", findValue(replaceContainer,
					Boolean.class));
		if (customHead != null)
			addParameter("customHead", findString(customHead));
		if (columns != null)
			addParameter("columns", findString(columns));
		if (dataset != null)
			addParameter("dataset", findString(dataset));
		if (loadURL != null)
			addParameter("loadURL", findString(loadURL));
		if (saveURL != null)
			addParameter("saveURL", findString(saveURL));
		if (language != null)
			addParameter("language", findString(language));
		if (skin != null)
			addParameter("skin", findString(skin));
		if (toolbarPosition != null)
			addParameter("toolbarPosition", findString(toolbarPosition));
		if (toolbarContent != null)
			addParameter("toolbarContent", findString(toolbarContent));
		if (resizable != null)
			addParameter("resizable", findValue(resizable, Boolean.class));
		if (minWidth != null)
			addParameter("minWidth", findValue(minWidth, Integer.class));
		if (minHeight != null)
			addParameter("minHeight", findValue(minHeight, Integer.class));
		if (showIndexColumn != null)
			addParameter("showIndexColumn", findValue(showIndexColumn,
					Boolean.class));
		if (stripeRows != null)
			addParameter("stripeRows", findValue(stripeRows, Boolean.class));
		if (lightOverRow != null)
			addParameter("lightOverRow", findValue(lightOverRow, Boolean.class));
		if (defaultRecord != null)
			addParameter("defaultRecord", findString(defaultRecord));
		if (transparentMask != null)
			addParameter("transparentMask", findValue(transparentMask,
					Boolean.class));
		if (clickStartEdit != null)
			addParameter("clickStartEdit", findValue(clickStartEdit,
					Boolean.class));
		if (reloadAfterSave != null)
			addParameter("reloadAfterSave", findValue(reloadAfterSave,
					Boolean.class));
		if (recountAfterSave != null)
			addParameter("recountAfterSave", findValue(recountAfterSave,
					Boolean.class));
		if (showGridMenu != null)
			addParameter("showGridMenu", findValue(showGridMenu, Boolean.class));
		if (allowCustomSkin != null)
			addParameter("allowCustomSkin", findValue(allowCustomSkin,
					Boolean.class));
		if (allowFreeze != null)
			addParameter("allowFreeze", findValue(allowFreeze, Boolean.class));
		if (allowHide != null)
			addParameter("allowHide", findValue(allowHide, Boolean.class));
		if (allowGroup != null)
			addParameter("allowGroup", findValue(allowGroup, Boolean.class));
		if (selectRowByCheck != null)
			addParameter("selectRowByCheck", findValue(selectRowByCheck,
					Boolean.class));
		if (submitUpdatedFields != null)
			addParameter("submitUpdatedFields", findValue(submitUpdatedFields,
					Boolean.class));

		if (remotePaging != null)
			addParameter("remotePaging", findValue(remotePaging, Boolean.class));
		if (pageSize != null)
			addParameter("pageSize", findValue(pageSize, Integer.class));
		if (pageSizeList != null)
			addParameter("pageSizeList", findString(pageSizeList));

		if (onClickCellTopics != null)
			addParameter("onClickCellTopics", findString(onClickCellTopics));
		if (onDblClickCellTopics != null)
			addParameter("onDblClickCellTopics", findString(onDblClickCellTopics));
		if (onClickHeadTopics != null)
			addParameter("onClickHeadTopics", findString(onClickHeadTopics));
		if (onCompleteTopics != null)
			addParameter("onCompleteTopics", findString(onCompleteTopics));
		if (onShowTopics != null)
			addParameter("onShowTopics", findString(onShowTopics));
		if (onResizeTopics != null)
			addParameter("onResizeTopics", findString(onResizeTopics));
		if (onMouseMoveTopics != null)
			addParameter("onMouseMoveTopics", findString(onMouseMoveTopics));
		if (beforeSelectRowTopics != null)
			addParameter("beforeSelectRowTopics", findString(beforeSelectRowTopics));
		if (afterSelectRowTopics != null)
			addParameter("afterSelectRowTopics", findString(afterSelectRowTopics));
		if (beforeEditTopics != null)
			addParameter("beforeEditTopics", findString(beforeEditTopics));
		if (afterEditTopics != null)
			addParameter("afterEditTopics", findString(afterEditTopics));
		if (beforeSaveTopics != null)
			addParameter("beforeSaveTopics", findString(beforeSaveTopics));
		if (beforeLoadTopics != null)
			addParameter("beforeLoadTopics", findString(beforeLoadTopics));
		if (beforeInsertTopics != null)
			addParameter("beforeInsertTopics", findString(beforeInsertTopics));
		if (afterInsertTopics != null)
			addParameter("afterInsertTopics", findString(afterInsertTopics));
		if (beforeUpdateTopics != null)
			addParameter("beforeUpdateTopics", findString(beforeUpdateTopics));
		if (afterUpdateTopics != null)
			addParameter("afterUpdateTopics", findString(afterUpdateTopics));
		if (beforeDeleteTopics != null)
			addParameter("beforeDeleteTopics", findString(beforeDeleteTopics));
		if (afterDeleteTopics != null)
			addParameter("afterDeleteTopics", findString(afterDeleteTopics));
		if (onUpdateRecordTopics != null)
			addParameter("onUpdateRecordTopics", findString(onUpdateRecordTopics));
		if (beforeGotoPageTopics != null)
			addParameter("beforeGotoPageTopics", findString(beforeGotoPageTopics));
	}

	@StrutsTagAttribute(description = "grid's id", required = true)
	public void setId(String id) {
		this.id = id;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	@StrutsTagAttribute(description = "grid's container", required = true)
	public void setContainer(String container) {
		this.container = container;
	}

	public void setReplaceContainer(String replaceContainer) {
		this.replaceContainer = replaceContainer;
	}

	public void setCustomHead(String customHead) {
		this.customHead = customHead;
	}

	@StrutsTagAttribute(description = "grid's column config", required = true)
	public void setColumns(String columns) {
		this.columns = columns;
	}

	@StrutsTagAttribute(description = "grid's dataset config", required = true)
	public void setDataset(String dataset) {
		this.dataset = dataset;
	}

	public void setLoadURL(String loadURL) {
		this.loadURL = loadURL;
	}

	public void setSaveURL(String saveURL) {
		this.saveURL = saveURL;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public void setSkin(String skin) {
		this.skin = skin;
	}

	public void setToolbarPosition(String toolbarPosition) {
		this.toolbarPosition = toolbarPosition;
	}

	public void setToolbarContent(String toolbarContent) {
		this.toolbarContent = toolbarContent;
	}

	public void setResizable(String resizable) {
		this.resizable = resizable;
	}

	public void setMinWidth(String minWidth) {
		this.minWidth = minWidth;
	}

	public void setMinHeight(String minHeight) {
		this.minHeight = minHeight;
	}

	public void setShowIndexColumn(String showIndexColumn) {
		this.showIndexColumn = showIndexColumn;
	}

	public void setStripeRows(String stripeRows) {
		this.stripeRows = stripeRows;
	}

	public void setLightOverRow(String lightOverRow) {
		this.lightOverRow = lightOverRow;
	}

	public void setDefaultRecord(String defaultRecord) {
		this.defaultRecord = defaultRecord;
	}

	public void setTransparentMask(String transparentMask) {
		this.transparentMask = transparentMask;
	}

	public void setClickStartEdit(String clickStartEdit) {
		this.clickStartEdit = clickStartEdit;
	}

	public void setReloadAfterSave(String reloadAfterSave) {
		this.reloadAfterSave = reloadAfterSave;
	}

	public void setRecountAfterSave(String recountAfterSave) {
		this.recountAfterSave = recountAfterSave;
	}

	public void setShowGridMenu(String showGridMenu) {
		this.showGridMenu = showGridMenu;
	}

	public void setAllowCustomSkin(String allowCustomSkin) {
		this.allowCustomSkin = allowCustomSkin;
	}

	public void setAllowFreeze(String allowFreeze) {
		this.allowFreeze = allowFreeze;
	}

	public void setAllowHide(String allowHide) {
		this.allowHide = allowHide;
	}

	public void setAllowGroup(String allowGroup) {
		this.allowGroup = allowGroup;
	}

	public void setSelectRowByCheck(String selectRowByCheck) {
		this.selectRowByCheck = selectRowByCheck;
	}

	public void setSubmitUpdatedFields(String submitUpdatedFields) {
		this.submitUpdatedFields = submitUpdatedFields;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageSizeList(String pageSizeList) {
		this.pageSizeList = pageSizeList;
	}

	public void setRemotePaging(String remotePaging) {
		this.remotePaging = remotePaging;
	}

	@StrutsTagAttribute(description = "Topics that published on click grid's cell.")
	public void setOnClickCellTopics(String onClickCellTopics) {
		this.onClickCellTopics = onClickCellTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on double click grid's cell.")
	public void setOnDblClickCellTopics(String onDblClickCellTopics) {
		this.onDblClickCellTopics = onDblClickCellTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on click grid's head.")
	public void setOnClickHeadTopics(String onClickHeadTopics) {
		this.onClickHeadTopics = onClickHeadTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on complete of grid.")
	public void setOnCompleteTopics(String onCompleteTopics) {
		this.onCompleteTopics = onCompleteTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on show grid.")
	public void setOnShowTopics(String onShowTopics) {
		this.onShowTopics = onShowTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on resize grid.")
	public void setOnResizeTopics(String onResizeTopics) {
		this.onResizeTopics = onResizeTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on mouse move.")
	public void setOnMouseMoveTopics(String onMouseMoveTopics) {
		this.onMouseMoveTopics = onMouseMoveTopics;
	}
	
	@StrutsTagAttribute(description = "Topics that published before select rows.")
	public void setBeforeSelectRowTopics(String beforeSelectRowTopics) {
		this.beforeSelectRowTopics = beforeSelectRowTopics;
	}

	@StrutsTagAttribute(description = "Topics that published after select rows.")
	public void setAfterSelectRowTopics(String afterSelectRowTopics) {
		this.afterSelectRowTopics = afterSelectRowTopics;
	}

	@StrutsTagAttribute(description = "Topics that published before edit data.")
	public void setBeforeEditTopics(String beforeEditTopics) {
		this.beforeEditTopics = beforeEditTopics;
	}
	
	@StrutsTagAttribute(description = "Topics that published after edit data.")
	public void setAfterEditTopics(String afterEditTopics) {
		this.afterEditTopics = afterEditTopics;
	}

	@StrutsTagAttribute(description = "Topics that published before save data.")
	public void setBeforeSaveTopics(String beforeSaveTopics) {
		this.beforeSaveTopics = beforeSaveTopics;
	}

	@StrutsTagAttribute(description = "Topics that published before load data.")
	public void setBeforeLoadTopics(String beforeLoadTopics) {
		this.beforeLoadTopics = beforeLoadTopics;
	}

	@StrutsTagAttribute(description = "Topics that published before insert records.")
	public void setBeforeInsertTopics(String beforeInsertTopics) {
		this.beforeInsertTopics = beforeInsertTopics;
	}
	
	@StrutsTagAttribute(description = "Topics that published after insert records.")
	public void setAfterInsertTopics(String afterInsertTopics) {
		this.afterInsertTopics = afterInsertTopics;
	}

	@StrutsTagAttribute(description = "Topics that published before update records.")
	public void setBeforeUpdateTopics(String beforeUpdateTopics) {
		this.beforeUpdateTopics = beforeUpdateTopics;
	}

	@StrutsTagAttribute(description = "Topics that published after update records.")
	public void setAfterUpdateTopics(String afterUpdateTopics) {
		this.afterUpdateTopics = afterUpdateTopics;
	}
	
	@StrutsTagAttribute(description = "Topics that published before delete records.")
	public void setBeforeDeleteTopics(String beforeDeleteTopics) {
		this.beforeDeleteTopics = beforeDeleteTopics;
	}

	@StrutsTagAttribute(description = "Topics that published after delete record.")
	public void setAfterDeleteTopics(String afterDeleteTopics) {
		this.afterDeleteTopics = afterDeleteTopics;
	}

	@StrutsTagAttribute(description = "Topics that published on update records.")
	public void setOnUpdateRecordTopics(String onUpdateRecordTopics) {
		this.onUpdateRecordTopics = onUpdateRecordTopics;
	}
	
	@StrutsTagAttribute(description = "Topics that published before go to page.")
	public void setBeforeGotoPageTopics(String beforeGotoPageTopics) {
		this.beforeGotoPageTopics = beforeGotoPageTopics;
	}

}
